from pydantic import BaseModel, Field, field_validator
from datetime import datetime
from typing import Optional, List


class NotebookBase(BaseModel):
    """笔记本基础模型"""
    name: str = Field(..., min_length=1, max_length=100)
    parent_id: Optional[int] = None


class NotebookCreate(NotebookBase):
    """笔记本创建模型"""
    pass


class NotebookUpdate(BaseModel):
    """笔记本更新模型"""
    name: Optional[str] = Field(None, min_length=1, max_length=100)
    parent_id: Optional[int] = None


class NotebookResponse(NotebookBase):
    """笔记本响应模型"""
    id: int
    user_id: int
    created_at: datetime
    
    class Config:
        from_attributes = True


class NotebookTreeResponse(NotebookResponse):
    """笔记本树形响应"""
    children: List['NotebookTreeResponse'] = []
    
    @field_validator('children', mode='before')
    @classmethod
    def ensure_children_list(cls, v):
        """确保 children 字段始终是列表"""
        if v is None:
            return []
        return v
    
    class Config:
        from_attributes = True

